layui.use(['form', 'layer'], function(){
    var form = layui.form
        ,layer = layui.layer;

    form.on('submit(addUser)', function(data){
        var roleIds = "";
        $("input:checkbox[name='roleId']:checked").each(function() { // 遍历name=test的多选框
            roleIds += $(this).val()+","; // 每一个被选中项的值
        });

        $.ajax({
            url: "admin/addOrUpdateUser",
            data: {
                id:data.field.userId,
                nickname: data.field.nickname,
                userName:data.field.userName,
                passWord:data.field.password,
                email:data.field.email,
                sex: data.field.sex,
                city:data.field.city,
                signMsg:data.field.signMsg,
                roleIds:roleIds
            },
            type: "POST",
            success: function(response){
                if(response.errNo == 0){
                    layer.alert(response.errMsg, {
                        icon: 1, yes: function () {
                            parent.layui.table.reload('userList');
                            parent.layer.closeAll('iframe'); //再执行关闭
                        }
                    })
                }else {
                    layer.alert(response.errMsg, {icon: 2});
                }
            },
            error: function () {
                parent.layer.alert("请求异常！", {icon: 2});
            }
        });
        return false;
    })
})